package io.github.mayubao.kuaichuan.micro_server;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by MJS on 2018/1/2.
 */

public class UpdateResUriHandler implements ResUriHandler {


    /**
     * 请求头的信息
     */
    private Map<String, String> headers = new HashMap<>();

    /**
     * 参数信息
     */
    private Map<String, String> parameters = new HashMap<>();

    public static final String UPDATE_PREFIX = "/upload/";

    private Activity mActivity;

    public UpdateResUriHandler(Activity activity) {
        mActivity = activity;
    }

    @Override
    public boolean matches(String uri) {
        return uri.startsWith(UPDATE_PREFIX);
    }

    @Override
    public void handler(Request request) {
        String uri = request.getUri();
        Socket socket = request.getUnderlySocket();
        OutputStream os = null;
        PrintStream printStream = null;
        InputStream is = null;
        try {
            os = socket.getOutputStream();
            printStream = new PrintStream(os);

            //printStream是输出流.上传的话,得到输入流,并且将文件存在本地.
            is = socket.getInputStream();
            LineNumberReader br = new LineNumberReader(new InputStreamReader(is));

            StringBuffer sb = new StringBuffer();
            String str = null;
            //读取请求头
            while ((str = br.readLine()) != null) {
                if ("".equals(str)) {
                    break;
                }
                if (!TextUtils.isEmpty(str)) {
                    if (str.indexOf(":") > 0) {
                        String[] strs = str.split(":");
                        headers.put(strs[0].toLowerCase(), strs[1].trim());
                    }
                }
                sb.append(str).append("\n");
            }
            //POST请求，Content-type为 multipart/form-data
            String contentType = null;
            if (/*"POST".equals(method) && */((contentType = headers.get("content-type")) != null
                    && headers.get("content-type").startsWith("multipart/form-data"))) {
                //文件上传的分割位 这里只处理单个文件的上传
                String boundary = contentType.substring(contentType.indexOf("boundary") +
                        "boundary=".length());
                //解析消息体
                while ((str = br.readLine()) != null) {
                    //解析结束的标记
                    do {
                        //读取boundary中的内容
                        //读取Content-Disposition
                        str = br.readLine();
                        //说明是文件上传
                        if (str.indexOf("Content-Disposition:") >= 0 && str.indexOf("filename") > 0) {
                            str = str.substring("Content-Disposition:".length());
                            String[] strs = str.split(";");
                            String fileName = strs[strs.length - 1].replace("\"", "").split("=")[1];
                          Log.d("taxi","fileName = " + fileName);
                            //这一行是Content-Type
                            br.readLine();
                            //这一行是换行
                            br.readLine();
                            //正式去读文件的内容
                            BufferedWriter bw = null;
                            try {
                                bw = new BufferedWriter(new OutputStreamWriter(new
                                        FileOutputStream("G:\\LearnVideo\\fileLoad" +
                                        File.separator + fileName), "UTF-8"));
                                while (true) {
                                    str = br.readLine();
                                    if (str.startsWith("--" + boundary)) {
                                        break;
                                    }
                                    bw.write(str);
                                    bw.newLine();
                                }
                                bw.flush();
                            } catch (Exception e) {

                            } finally {
                                if (bw != null) {
                                    bw.close();
                                }
                            }
                        }
                        if (str.indexOf("Content-Disposition:") >= 0) {
                            str = str.substring("Content-Disposition:".length());
                            String[] strs = str.split(";");
                            String name = strs[strs.length - 1].replace("\"", "").split("=")[1];
                            br.readLine();
                            StringBuilder stringBuilder = new StringBuilder();
                            while (true) {
                                str = br.readLine();
                                if (str.startsWith("--" + boundary)) {
                                    break;
                                }
                                stringBuilder.append(str);
                            }
                            parameters.put(name, stringBuilder.toString());
                        }
                    } while (("--" + boundary).equals(str));
                    //解析结束
                    if (str.equals("--" + boundary + "--")) {
                        break;
                    }
                }
            }
            //System.out.println(sb.toString());
            //获取URI

//            uri = StringUtils.parserUri(sb.toString(), " ");
            uri = sb.toString();

            Log.d("taxi", "解析的uri=" + uri);
            int flag = -1;
            //说明有参数
            if ((flag = uri.indexOf('?')) >= 0) {
                String oldUri = uri;
                uri = uri.substring(0,flag);
                String parameterPath = oldUri.substring(flag+1);
                String[] parameter = parameterPath.split("&");
                if (parameter != null && parameter.length > 0) {
                    for (int i = 0; i < parameter.length; i++) {
                        String str1 = parameter[i];
                        if((flag = str1.indexOf('=')) >= 0){
                            String key = str1.substring(0,flag);
                            String value = str1.substring(flag+1);
                            parameters.put(key,value);
                        }else{
                            parameters.put(str,null);
                        }
                    }
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void destroy() {
        mActivity = null;
    }
}
